package com.hotel.search.lucene;

import java.io.IOException;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * lucene搜索引擎工厂类
 * 工厂类会初始化搜索引擎池，默认初始化大小10，最大搜索引擎大小为1000
 *
 * @author XuSemon
 * @date 2015-4-30
 */
public class LuceneSearchFactory {
    /**
     * IndexSearch
     */
    private Queue<LuceneSearch> searchs = new LinkedBlockingQueue<LuceneSearch>();
    /**
     * 最小初始化数量
     */
    private int minSize;
    /**
     * 最大初始化数量
     */
    private int maxSize;
    /**
     * 目前使用中的数量
     */
    private int num = 0;
    /**
     * 索引文件路径
     */
    private String fsPath;

    /**
     * 构造函数
     *
     * @param fsPath
     */
    public LuceneSearchFactory(String fsPath) {
        this(fsPath, 10, 1000);
    }

    public LuceneSearchFactory(String fsPath, int minSize, int maxSize) {
        this.fsPath = fsPath;
        this.minSize = minSize;
        this.maxSize = maxSize;
    }

    /**
     * 生成指定目录的搜索类
     *
     * @param fsPath
     * @return
     */
    public LuceneSearch getSearch() {
        synchronized (searchs) {
            try {
                if (searchs.size() > 0) {
                    ++num;
                    return searchs.poll();
                }
                if (maxSize == 0 || num < maxSize) {
                    ++num;
                    return create();
                } else {
                    throw new RuntimeException("The search in cache is empty!");
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    private LuceneSearch create() throws IOException {
        return new LuceneSearch(fsPath);
    }

    public void releaseSearch(LuceneSearch search) {
        synchronized (searchs) {
            if (searchs.size() == minSize) {
                num--;
                search.close();
                search = null;
                return;
            }
            searchs.offer(search);
            num--;
        }
    }

    public void clearPosition() {
        getPositionSearchFactory().getSearchs().clear();
        num = 0;
    }

    public String getFsPath() {
        return fsPath;
    }

    public void setFsPath(String fsPath) {
        this.fsPath = fsPath;
    }

    private static LuceneSearchFactory positionSearchFactory;
    private static LuceneSearchFactory personSearchFactory;

    /**
     * 获取职位搜索工厂类
     *
     * @return
     */
    public synchronized static LuceneSearchFactory getPositionSearchFactory() {
        if (positionSearchFactory == null) {
            positionSearchFactory = new LuceneSearchFactory(LuceneProp.POSITIONPATH);
        }
        return positionSearchFactory;
    }

    /**
     * 获取个人搜索工厂类
     *
     * @return
     */
    public synchronized static LuceneSearchFactory getPersonSearchFactory() {
        if (personSearchFactory == null) {
            personSearchFactory = new LuceneSearchFactory(LuceneProp.PERSONPATH);
        }
        return personSearchFactory;
    }

    public Queue<LuceneSearch> getSearchs() {
        return searchs;
    }

    public void setSearchs(Queue<LuceneSearch> searchs) {
        this.searchs = searchs;
    }

}
